$(function(){	
	var draftAddControl = $(".custom-box-add-control");
	var draftAdd = $(".custom-box-add");
	var draftList = $(".custom-list");
	var bookid = getSearchValue("bookid","pageparam_draft");
	var bookname = getSearchValue("bookname","pageparam_draft");
  	var publishbookid = getSearchValue("publishbookid","pageparam_draft") || "";
	checkLog();
	createBarBread([{title:"首页",url:indexUrl},
		{title:"我的小说",url:"mybook-list.html"},
		{title:bookname,url:"mybook-detail.html",urlparam:"bookid=" + bookid + "&bookname=" + bookname+"&publishbookid="+publishbookid},
		{title:"草稿箱"}]);
	draftAddControl.find("a").on("click",function(){
		$(this).parent().hide();
		$(".custom-box-add").show();
	});
	draftAddControl.find("a").on("click",function(){
		$(this).parent().hide();
		$(".custom-box-add").show();
	});
	draftAdd.find(".del").on("click",function(){
		hideDraftBox()
	});
	draftAdd.find(".save").on("click",function(){
		var tle = draftAdd.find("input").val();
		var con = draftAdd.find("textarea").val();
		if(tle == ""){
			layer.open({
			    content: '请输入自定义标题',
			    time: 1
			});
			return;
		}
		baseLoad();
		$.ajax({
			url:APIURL + "/Novel/createNovelIdea.php",
			data:"userId=" + userId() + "&bookId=" + bookid + "&title=" + encodeURIComponent(tle) + "&content=" + encodeURIComponent(con),
			type:"post",
			success:function(data){
				hideBaseLoad();
				data = $.parseJSON(data);
		        if(data.status != "FAILED"){
		        	layer.open({
		              content: "已添加",
		              time: 1
		          	}); 
					var addStr = createItem(tle,con,data.value);
					draftList.append(addStr);
					hideDraftBox()		                        
		        }else{
		          layer.open({
		              content: data.value,
		              time: 1
		          }); 
		        }

			},
			error:function(err){
				hideBaseLoad();
				layer.open({
				    content: err.value || err_txt,
				    time: 2
				});
			}

		})

	});
	initDraft();
	function initDraft(){
		baseLoad();
		$.ajax({
			url:APIURL + "/Novel/getNovelIdeaListWeb.php",
			data:"userId=" + userId() + "&bookId="+ bookid + "&page=0&size=10000",
			success:function(data){	
				data = $.parseJSON(data);
			    hideBaseLoad();
	            if(data.status != "FAILED"){
					var str = "";
					var d = data.value;
					for(var i = 0, len = d.length; i < len; i++){
						str += createItem(d[i].title,d[i].content,d[i].id);
					}
					draftList.html(str);                        
		        }else{
		          layer.open({
		              content: data.value,
		              time: 1
		          }); 
		        }		

			},
			error:function(){
				hideBaseLoad();
		        layer.open({
		            content: err.value || err_txt,
		            time: 2
		        });
			}
		})
	}

	function createItem(tle,con,id){
		return  '<div class="custom-item" data-id="'+id+'">'+
					'<input type="text" placeholder="自定义标题" value="'+decodeURIComponent(tle)+'">'+
					'<textarea name="" id="" cols="20" rows="5" placeholder="自定义内容" style="resize:vertical">'+decodeURIComponent(con)+'</textarea>'+
					'<div class="btns">'+
						'<a href="javascript:;" class="save">备份到云端</a><a href="javascript:;" class="del hide">删除</a>'+
					'</div>'+
				'</div>';
	}

	function hideDraftBox(){
		draftAddControl.show();
		draftAdd.hide();
		draftAdd.find("input").val("");
		draftAdd.find("textarea").val("");
	}

	draftList.on("click",".save",function(){
		var self = this;
		var subTle = $(this).parent().siblings("input").val();
		var subCon = $(this).parent().siblings("textarea").val();
		if(subTle == ""){
			layer.open({
			    content: '请输入标题',
			    time: 1
			});
			return;
		}
		layer.open({
		  title: '提示',
		  content: '备份将覆盖以前备份的内容,您确定要备份吗?',
		  btn: ['嗯', '不要'],
		  yes: function(index){
		    layer.close(index);
		    saveDraft();
		  }
		});
		function saveDraft(){
			baseLoad();
			$.ajax({
				url:APIURL + "/Novel/updateNovelIdea.php",
				data:"userId=" + userId() + "&remoteId=" + $(self).parents(".custom-item").attr("data-id") + "&title=" + encodeURIComponent(subTle) + "&content=" + encodeURIComponent(subCon),
				type:"post",
				success:function(data){
					hideBaseLoad();
					data = $.parseJSON(data);
			        if(data.status != "FAILED"){
				        layer.open({
						    content: "已备份",
						    time: 1
						});                
			        }else{
			          layer.open({
			              content: data.value,
			              time: 1
			          }); 
			        }

				},
				error:function(err){
					hideBaseLoad();
					layer.open({
					    content: err.value || err_txt,
					    time: 2
					});
				}

			})			
		}
		
	})
	//删除操作
	draftList.on("click",".del",function(){
		var self = this;
		var custom = $(self).parents(".custom-item");
		layer.open({
		    title: '提示',
		    content: '您确定要删除此自定义项吗？',
		    btn: ['嗯', '不要'],
		    yes: function(index){
				$.ajax({
					url:APIURL + "/Novel/deleteIdeaWeb.php",
					data:"userId=" + userId() + "&remoteId=" + custom.attr("data-id"),
					success:function(data){
						data = $.parseJSON(data);
					    hideBaseLoad();
			            if(data.status != "FAILED"){
					    	//删除成功回调
					    	layer.open({
				              content: "已删除",
				              time: 1
				          }); 
					    	custom.fadeOut();                      
				        }else{
				          layer.open({
				              content: data.value,
				              time: 1
				          }); 
				        }		

					},
					error:function(){
						hideBaseLoad();
				        layer.open({
				            content: err.value || err_txt,
				            time: 2
				        });
					}
				})
		        layer.close(index);
		    }
		});
	})
	loadGuide();
	set_tab_indent_for_textareas();

})